package com.quectel.dms.controller.emq;

import cn.hutool.crypto.digest.MD5;
import com.quectel.constant.sys.SysConfigKeyConstants;
import com.quectel.dms.controller.BaseController;
import com.quectel.sdk.emq.bean.EmqMsgBean;
import com.quectel.sys.module.sys.service.SysConfigService;
import com.quectel.util.common.JacksonUtils;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * 消息接收
 *
 * @author rananxin
 * @email: ryan.ran@quectel.com
 * @date 2021/11/18 4:04 下午
 */
@RestController
@RequestMapping("emqCollect")
@Tag(name = "EmqCollectController")
@Slf4j
public class EmqCollectController extends BaseController {

    @DubboReference
    private SysConfigService sysConfigService;

    @PostMapping("receiveData")
    public Response<Object> receiveData(@RequestBody EmqMsgBean emqMsgBean, @RequestHeader("token") String token) {

        String commonSecret = sysConfigService.getConfigValueByKey(SysConfigKeyConstants.COMMON_SECRET);
        if (StringUtils.isBlank(commonSecret) || !MD5.create().digestHex(commonSecret).equals(token)) {
            return Response.error("token error");
        }

        logger.info("data from emq {}", JacksonUtils.toJsonString(emqMsgBean));

        // 进出数据处理流程

        return Response.ok();
    }

}
